<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="https://d3js.org/d3.v5.min.js"></script>
  <style>
    #app {
      margin: 0 auto;
      width: 500px;
      height: 400px;
      background: #efefef;
    }

    .bar {
      fill: green;
    }
  </style>
</head>

<body>
  <svg id="app"></svg>

  <script>
    const SVG_HEIGHT = 400;
    const SVG_WIDTH = 500;
    const MARGIN = { TOP: 30, RIGHT: 30, LEFT: 30, BOTTOM: 30 }

    var datalist = [20, 30, 40, 50, 15];

    var container = d3.select("#app");

    //序数比例尺
    const xScale = d3.scaleBand()
      .domain(d3.range(datalist.length)) //[0,1,2,3,4]
      .range([0, SVG_WIDTH - MARGIN.LEFT - MARGIN.RIGHT])
      .paddingInner(0.1)

    //线性比例尺
    const yScale = d3.scaleLinear()
      .domain([0, d3.max(datalist)])
      .range([SVG_HEIGHT - MARGIN.TOP - MARGIN.BOTTOM, 0])

    //水平方向坐标轴
    var axisBottom = d3.axisBottom(xScale)
    //  container.append('g').call(axisBottom)
    axisBottom(
      container.append('g')
        .attr('class', 'xaxis')
        .attr("transform", `translate(${MARGIN.LEFT},${SVG_HEIGHT - MARGIN.TOP})`)
    )

    //垂直方向坐标轴
    var axisLeft = d3.axisLeft(yScale)
    axisLeft(
      container.append("g")
        .attr("transform", `translate(${MARGIN.LEFT},${MARGIN.TOP})`)
    )

    //更新水平方向tick值
    let ticks = ["北京", "天津", "上海", "海口", "昌平"];
    d3.select('.xaxis').selectAll('text').data(ticks)
      .text((d) => {
        return d;
      })

    container.selectAll('rect')
      .data(datalist)
      .enter()
      .append('rect')
      // .attr('class','bar')
      .classed('bar', true)
      .attr('x', function (d, i) {
        return xScale(i) + MARGIN.LEFT;
      })
      .attr('y', function (d, i) {
        return SVG_HEIGHT - MARGIN.TOP;
      })
      .attr('width', function (d, i) {
        return xScale.bandwidth();
      })
      .attr('height', function (d) {
        return 0;
      })
      .transition()
      .duration(1000)
      .delay(function (d, i) {
        return 500 * i;
      })
      .attr('y', function (d, i) {
        return yScale(d) + MARGIN.TOP;
      })
      .attr('height', function (d) {
        return SVG_HEIGHT - MARGIN.TOP - MARGIN.BOTTOM - yScale(d);
      })

    container.append('g').attr('class', 'textGrop')
    container.select('.textGrop')
      .selectAll('text')
      .data(datalist)
      .enter()
      .append('text')
      .attr("text-anchor", "middle")
      .text(function (d, i) {
        return d;
      })
      .attr('x', function (d, i) {
        return xScale(i) + MARGIN.LEFT + xScale.bandwidth() / 2;
      })
      .attr('y', function (d, i) {
        return SVG_HEIGHT - MARGIN.TOP - 10;
      })
      .transition()
      .duration(1000)
      .delay(function (d, i) {
        return 500 * i;
      })
      .attr('x', function (d, i) {
        return xScale(i) + MARGIN.LEFT + xScale.bandwidth() / 2;
      })
      .attr('y', function (d, i) {
        return yScale(d) + MARGIN.TOP - 10;
      })



  </script>

</body>

</html>